﻿using System;
using System.Web.Services;
using System.IO;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
using CommonFunc.BLL;
using WF_Business;

/// <summary>
/// Summary description for Scanner
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Scanner : System.Web.Services.WebService
{
    public Scanner()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string[] GetLb()
    {
        string[] dss = ConfigurationManager.AppSettings["fjlb"].ToString().Split(',');
        return dss;
    }

    [WebMethod]
    public string[][] GetEloamImg(String IID)
    {
        string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
        WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
        System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[1];
        idp[0] = new OracleParameter(":IID", OracleType.VarChar);
        idp[0].Value = IID;

        string sql = string.Format("select * from {0} where IID = :IID", fjTabName);
        DataTable dt = idaTemp.RunSql(sql, ref idp);
        string[][] arrArrays = new string[dt.Rows.Count][];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            arrArrays[i] = new string[3];
            arrArrays[i][0] = Convert.ToBase64String(dt.Rows[i]["DATA"] as byte[]);
            arrArrays[i][1] = Convert.ToString(dt.Rows[i]["ext_type"]);
            arrArrays[i][2] = Convert.ToString(dt.Rows[i]["Name"]);
        }
        return arrArrays;
    }

    [WebMethod]
    public void UploadScan(byte[] data, String IID, String userId, String lb)
    {
        string fjTabName = ConfigurationManager.AppSettings["fjTabName"];

        Guid fileID = Guid.NewGuid();
        string path = fileID.ToString() + ".jpg";
        WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
        System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[5];
        idp[0] = new OracleParameter(":data", OracleType.Blob);
        idp[1] = new OracleParameter(":path", OracleType.VarChar);
        idp[2] = new OracleParameter(":IID", OracleType.VarChar);
        idp[3] = new OracleParameter(":LB", OracleType.VarChar);

        idp[0].Value = data;
        idp[1].Value = path;
        idp[2].Value = IID;
        idp[3].Value = lb;

        string sql = string.Format("insert into {0}(DATA, NAME, IID, ext_type) values(:data,:path,:IID,:lb)", fjTabName);
        idaTemp.RunSql(sql, ref idp);
    }

    [WebMethod]
    public bool DeleteImage(string iid, string name, string lb)
    {
        try
        {
            string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
            string sql = string.Format("delete from {0} where iid='{1}' and name='{2}' and ext_type='{3}'", fjTabName, iid, name, lb);
            return SysParams.OAConnection().RunSql(sql) > 0;
        }
        catch (Exception)
        {
            return false;
        }
    }

    [WebMethod]
    public void UploadEloam(byte[] data, String IID, String userId, String lb, String fileName)
    {
        string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
        string tbNameSQB = ConfigurationManager.AppSettings["tbNameSQB"];
        WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection(true);
        System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[5];
        idp[0] = new OracleParameter(":data", OracleType.Blob);
        idp[1] = new OracleParameter(":path", OracleType.VarChar);
        idp[2] = new OracleParameter(":IID", OracleType.VarChar);
        idp[3] = new OracleParameter(":LB", OracleType.VarChar);
        idp[4] = new OracleParameter(":userid", OracleType.VarChar);
        idp[0].Value = data;
        idp[1].Value = fileName;
        idp[2].Value = IID;
        idp[3].Value = lb;
        idp[4].Value = userId;
        try
        {
            //string sql = " insert into st_user_sign ( userid, savedate, signpic, magicnum ) values ( :userid, :savedate, :signpic, :magicnum )";
            string sql = string.Format("insert into {0}(EXT_NAME,DATA, NAME, IID, EXT_TYPE, USERID) values('.jpg',:data,:path,:IID,:lb, :userid)", fjTabName);
            idaTemp.RunSql(sql, ref idp);
            sql = string.Format("update {1} set zt='2' where iid='{0}' and userid='{2}'", IID, tbNameSQB, userId);
            idaTemp.RunSql(sql);
            idaTemp.Close(true);
        }
        catch (Exception ex)
        {
            Common.Log.LogManage.addLogErr("档案入库出错", ex.Message);
            idaTemp.Close(false);
        }
    }

    [WebMethod]
    public string UploadFileToFtp(String ftpUrl, String userName, String passWord, byte[] data, String fileName, String IID, String userId, String lb, String ftpDirctory)
    {
        var strMsg = string.Empty;
        var flag = FtpBLL.Instance.CopyFileToFtp(ftpUrl, userName, passWord, IID, fileName, lb, data, ftpDirctory, userId, ref strMsg);
        if (string.IsNullOrWhiteSpace(strMsg) && !flag)
        {
            strMsg = "附件表数据写入失败";
        }
        return strMsg;
    }
    /// <summary>
    /// 读文件到byte[]
    /// </summary>
    /// <param name="fileName">硬盘文件路径</param>
    /// <returns></returns>
    public byte[] ReadFileToByte(string fileName)
    {
        FileStream pFileStream = null;
        byte[] pReadByte = new byte[0];
        try
        {
            pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader r = new BinaryReader(pFileStream);
            r.BaseStream.Seek(0, SeekOrigin.Begin);    //将文件指针设置到文件开
            pReadByte = r.ReadBytes((int)r.BaseStream.Length);
            return pReadByte;
        }
        catch
        {
            return pReadByte;
        }
        finally
        {
            if (pFileStream != null)
                pFileStream.Close();
        }
    }

    [WebMethod]
    public DataSet GetSQInfoData(string name, string cardid)
    {
        string ryTabName = ConfigurationManager.AppSettings["ryTabName"];
        WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
        DataSet ds = new DataSet();
        string sql = " select f.IID,f.FAMILY_NAME,f.FAMILY_ZJHM,FWJZMJ,case when f.zt='2' then '已上传' else '未上传' end zt from " + ryTabName + " f  where f.zt in ('1','2')";
        if (!string.IsNullOrWhiteSpace(name))
        {
            sql += " and f.FAMILY_NAME like '%" + name + "%' ";
        }
        if (!string.IsNullOrWhiteSpace(cardid))
        {
            sql += " and f.FAMILY_ZJHM  like '%" + cardid + "%' ";
        }
        sql += " order by f.iid desc ";
        idaTemp.RunSql(sql, out ds);
        return ds;
    }

    [WebMethod]
    public bool DownloadFileForDirectory(string ftpUrl, string userName, string passWord, string iid, string LocalPath, string ftpDirctory)
    {
        return FtpBLL.Instance.DownloadFileForDirectory(ftpUrl, userName, passWord, iid, LocalPath, ftpDirctory);
    }

    [WebMethod]
    public bool DeleteFtpFile(string ftpUrl, string userName, string passWord, string remoteDirectoryName, string remoteFileName)
    {
        return FtpBLL.Instance.DeleteFtpFile(ftpUrl, userName, passWord, remoteDirectoryName, remoteFileName);
    }

    //[WebMethod]
    //public DataSet LoadScan()
    //{
    //    string ryTabName = ConfigurationManager.AppSettings["ryTabName"];
    //    WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
    //    DataSet ds = new DataSet();
    //    string sql = "select IID from " + ryTabName;
    //    idaTemp.RunSql(sql, out ds);
    //    return ds;
    //}

    //[WebMethod]
    //public DataSet SelectIID(string IID)
    //{
    //    string ryTabName = ConfigurationManager.AppSettings["ryTabName"];
    //    WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
    //    System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[1];
    //    idp[0] = new OracleParameter(":IID", OracleType.VarChar);
    //    idp[0].Value = '%' + IID + '%';

    //    string sql = string.Format("select IID  from {0} where IID like :IID ", ryTabName);
    //    DataSet ds = new DataSet();
    //    ds.Tables.Add(idaTemp.RunSql(sql, ref idp));
    //    return ds;
    //}

    //[WebMethod]
    //public string[][] GetImg(String IID)
    //{
    //    string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
    //    WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
    //    System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[1];
    //    idp[0] = new OracleParameter(":IID", OracleType.VarChar);
    //    idp[0].Value = IID;

    //    string sql = string.Format("select * from {0} where IID = :IID ", fjTabName);
    //    DataTable dt = idaTemp.RunSql(sql, ref idp);
    //    string[][] arrArrays = new string[dt.Rows.Count][];
    //    for (int i = 0; i < dt.Rows.Count; i++)
    //    {
    //        arrArrays[i] = new string[3];
    //        arrArrays[i][0] = Convert.ToBase64String(dt.Rows[i]["DATA"] as byte[]);
    //        arrArrays[i][1] = Convert.ToString(dt.Rows[i]["LB"]);
    //        arrArrays[i][2] = Convert.ToString(dt.Rows[i]["ID"]);
    //    }
    //    return arrArrays;
    //}

    //[WebMethod]
    //public void DelImgById(string Id)
    //{
    //    string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
    //    WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
    //    System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[1];
    //    idp[0] = new OracleParameter(":Id", OracleType.VarChar);

    //    idp[0].Value = Id;

    //    string sql = string.Format("delete from {0} where ID = :Id ", fjTabName);
    //    idaTemp.RunSql(sql, ref idp);
    //}

    //[WebMethod]
    //public int FindByIID(String IID, String lb)
    //{
    //    string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
    //    WF_DataAccess.IDataAccess idaTemp = WF_Business.SysParams.OAConnection();
    //    System.Data.IDataParameter[] idp = new System.Data.OracleClient.OracleParameter[2];
    //    idp[0] = new OracleParameter(":IID", OracleType.VarChar);
    //    idp[1] = new OracleParameter(":lb", OracleType.VarChar);
    //    idp[0].Value = IID;
    //    idp[1].Value = lb;
    //    string sql = string.Format("select * from {0} where iid = :IID and lb = :lb", fjTabName);
    //    DataTable ds = idaTemp.RunSql(sql, ref idp);
    //    if (ds.Rows.Count > 0)
    //        return 1;
    //    else
    //        return -1;
    //}

    //[WebMethod]
    //public byte[] GetByteData(string aid)
    //{
    //    string fjTabName = ConfigurationManager.AppSettings["fjTabName"];
    //    DataTable dtTemp;
    //    var strSql = "select data from " + fjTabName + " where id='" + aid + "'";
    //    SysParams.OAConnection().RunSql(strSql, out dtTemp);
    //    return (byte[])dtTemp.Rows[0]["data"];
    //}

    //[WebMethod]
    //public string Hello()
    //{
    //    return "111111";
    //}

    //[WebMethod]
    //public string UploadFileToFtpTest(String ftpUrl, String userName, String passWord, String fileName, String IID, String userId, String lb, String ftpDirctory)
    //{
    //    var strMsg = string.Empty;
    //    var data = ReadFileToByte(@"D:\投标助手图片\01.png");
    //    FtpBLL.Instance.CopyFileToFtp(ftpUrl, userName, passWord, IID, fileName, lb, data, ftpDirctory, userId, ref strMsg);
    //    return strMsg;
    //}
}
